package Game;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


public class Connection extends Thread {
	
	private Socket server;
	private boolean runs;
	private PrintWriter output;
	private BufferedReader input;
	
	public Connection (String Adresse, int port) {
		try {
			this.server = new Socket(Adresse, port);
			this.output = new PrintWriter(server.getOutputStream(), true);
			this.input = new BufferedReader(new InputStreamReader(server.getInputStream()));
			this.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Connection (Socket socket) {
		try {
			this.server = socket;
			this.output = new PrintWriter(server.getOutputStream(), true);
			this.input = new BufferedReader(new InputStreamReader(server.getInputStream()));
			this.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void run() {
		runs = true;
		while (runs) {
			try {
				String message = input.readLine();
				if (message != null) {
					Main.sendMessage(message);
				}
			} catch (Exception e) {
				runs = false;
			}
		}
	}
	
	public void sendMessage(String message) {
		output.println(message);
		output.flush();
	}
	
	public void close() {
		try {
			runs = false;
			server.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public boolean isRunning() {
		return runs;
	}
	
	public boolean newGame(Player.PlayerID id) {
		
		return false;
	}
	
	public boolean updateField(Player.PlayerID id, int x, int y) {
		return false;
	}

}

